/******************************************************************************
*	cmTask.h	Header file of CM Task Module
*
*	coypright (C) 2009 Ding Lixing
*
******************************************************************************/

#ifndef __CMTASK_INC__
#define __CMTASK_INC__

#ifdef __cplusplus
extern "C" {
#endif

struct cmTASK_typ {
    vmCONTEXT       ctx;
    cmLISTNODE      tNode;
    cmLISTNODE      pNode;
    unsigned char   flag;
    unsigned char   flagEx;
    unsigned char   reserved;
    unsigned char   priority;
    unsigned char   prioritySet;
    unsigned char   slice;
    unsigned char   sliceConsumed;
    unsigned char   condType;
    unsigned long   condWait;
    unsigned int    sleepTicks;
    unsigned int    wakeCapture;
    void *          pendItem;
    void *          exBlock;
    int             (*entry)(void *);
    void *          argument;
    void *          exit;
    void *          stack;
    unsigned int    stackSize;
};
typedef struct cmTASK_typ cmTASK;

#define cmTaskFlagTst(t, f) (((cmTASK *)(t))->flag & (f))
#define cmTaskFlagClr(t, f) (((cmTASK *)(t))->flag &= ~(f))
#define cmTaskFlagSet(t, f) (((cmTASK *)(t))->flag |= (f))

extern void cmTaskLibInit(void);
extern int  cmTaskInit
(
    cmTASK *        task,
    unsigned char   priority,
    unsigned char   slice,
    void *          stackAddr,
    unsigned int    stackSize,
    int             (*entry)(void *),
    void *          argument,
    unsigned int    option
);
extern int  cmTaskStart(cmTASK *task);
extern void cmTaskSleep(unsigned int tout);
extern int  cmTaskWake(cmTASK *task);
extern void cmTaskTransfer(void);
extern int  cmTaskPrioritySet(cmTASK *task, unsigned char priority);
extern int  cmTaskSuspend(cmTASK *task);
extern int  cmTaskResume(cmTASK *task);

struct cmOBJ_typ {
    unsigned int    type;
    unsigned int    opt;
    void *          data;
    int             (*prePost)(struct cmOBJ_typ *, void *);
    int             (*prePend)(struct cmOBJ_typ *, void *);
    cmLISTHEAD      waitList;
};
typedef struct cmOBJ_typ cmOBJ;

extern int cmTaskObjInit
(
    cmOBJ *obj,
    unsigned int type,
    unsigned int opt,
    int (*prePost)(cmOBJ *, void *),
    int (*prePend)(cmOBJ *, void *),
    void *data
);
extern int cmTaskObjPost(cmOBJ *obj, void *param);
extern int cmTaskObjPostEach(cmOBJ *obj, void *param, int(*callback)(cmOBJ *, cmTASK*));
extern int cmTaskObjPend(cmOBJ *obj, unsigned int tout, void *param);


#ifdef __cplusplus
}
#endif

#endif /* __CMTASK_INC__ */

